﻿using System;
using System.IO;
using ThoughtWorks.CruiseControl.Core.Tasks.Conditions;
using NUnit.Framework;
using Rhino.Mocks;
using ThoughtWorks.CruiseControl.Core.Util;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Tasks.Conditions
{
    [TestFixture]
    public class FolderExistsTaskConditionTest
    {
        MockRepository mocks = new MockRepository();
        IFileSystem fileSystem;

        [TestFixtureSetUp]
        public void testFixtureSetUp()
        {
            fileSystem = mocks.StrictMock<IFileSystem>();
            Expect.Call(fileSystem.DirectoryExists("c:\\testfolder\\")).Return(true).Repeat.Any();
            Expect.Call(fileSystem.DirectoryExists(null)).IgnoreArguments().Return(false).Repeat.Any();

            //IFunctions functions = mocks.StrictMock<IFunctions>();

            //Expect.Call(functions.DirectoryExists("c:\\testfolder\\")).IgnoreArguments().Do(new FolderExistsDelegate(Formal)).Repeat.Any();

            //Functions.Default = functions;

            mocks.ReplayAll();
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            mocks.VerifyAll();
        }

        delegate bool FolderExistsDelegate(string filepath);
        private bool Formal(string filepath)
        {
            var info = new DirectoryInfo(filepath);
            return info.FullName.Equals("c:\\testfolder\\", StringComparison.InvariantCultureIgnoreCase);
        }

        
        
        //[TestFixtureSetUp]
        //public void testFixtureSetUp()
        //{
        //    FunctionsMock functions;
        //    functions = new FunctionsMock();
        //    functions.FolderExistsResultArray.AddRange(new[] { "c:\\testfolder\\" });
        //    Functions.Default = functions;
        //}

        [Test]
        [ExpectedException(typeof(System.NullReferenceException))]
        public void nullFolderThrowsArguementException()
        {
            FolderExistsTaskCondition condition = new FolderExistsTaskCondition();
            condition.FolderName = null;
            condition.Eval(null);
        }

        [Test]
        public void BulkTest()
        {
            executeTest("c:\\TestFolder\\", true);
            executeTest("c:\\testFolder\\", true);
            executeTest("c:\\Testfolder\\", true);
            executeTest("c:\\Test\\..\\TestFolder\\", true);
            executeTest("c:\\test\\..\\TestFolder\\", true);
            executeTest("c:\\test2\\TestFolder\\", false);
            executeTest("c:\\TestFolder2\\", false);
        }

        private void executeTest(string testfolder, bool expected)
        {
            FolderExistsTaskCondition condition = new FolderExistsTaskCondition();
            condition.FolderName = testfolder;
            condition.FileSystem = fileSystem;
            string Message = string.Format("Validated '{0}' existed, Expected '{1}'", testfolder, expected);
            Assert.That(condition.Eval(null), Is.EqualTo(expected),Message);
        }
    }
}
